﻿using System;
using System.Linq;
using System.Web.Mvc;
using MiniWeb.Core.Entities;
using MiniWeb.Core.Enums;
using MiniWeb.Task;
using MiniWeb.Web.Controllers.Admin.OrdersManager.ViewModels;
using WFramework.Extensions;
using WFramework.Mappers;
using WFramework.MvcPager;
using WFramework;
using System.Collections.Generic;
namespace MiniWeb.Web.Controllers.Admin.OrdersManager
{

    public class OrdersManagerController : BaseController
    {
        private readonly OrdersTask _ordersTask = new OrdersTask();

        public ActionResult List(string account, string code, int? paytype, int? orderstate, DateTime? starttime, DateTime? endtime, int pageIndex = 1, int pageSize = 20)
        {
            var pagedList = _ordersTask.GetPagedList(account, code, paytype, orderstate, starttime, endtime, pageIndex, pageSize);
            ViewData["orderstatelist"] = OrderStateEnum.已支付.ToSelectListForAll();
            ViewData["paytypelist"] = PayTypeEnum.后台处理.ToSelectListForAll();

            var model = new PagedList<Orders>(pagedList.ToList(), pageIndex, pageSize, pagedList.TotalItemCount);

            return View(model);
        }


        public ActionResult Save(int? id)
        {
            ViewData["orderstatelist"] = OrderStateEnum.已支付.ToSelectListForAll();
            ViewData["paytypelist"] = PayTypeEnum.后台处理.ToSelectListForAll();

            var model = new SaveOrders();

            if (id != null)
            {
                var item = _ordersTask.GetById((int)id);
                if (item == null)
                    return AlertMsg("参数错误", HttpContext.Request.UrlReferrer.PathAndQuery);

                model = EntityMapper.Map<Orders, SaveOrders>(item);
            }
            return View(model);
        }

        [HttpPost]
        public ActionResult Save(SaveOrders savemodel)
        {
            ViewData["orderstatelist"] = OrderStateEnum.已支付.ToSelectListForAll();
            ViewData["paytypelist"] = PayTypeEnum.后台处理.ToSelectListForAll();
            if (ModelState.IsValid)
            {

                if (savemodel.ID == null)
                {

                }
                else
                {
                    var model = _ordersTask.GetById((int)savemodel.ID);

                    if (model == null)
                        return AlertMsg("参数错误", HttpContext.Request.UrlReferrer.PathAndQuery);

                    model.Code = savemodel.Code;
                    if (model.State == (int)OrderStateEnum.已支付)
                    {
                        model.FinishDate = DateTime.Now;
                        model.PayType = (int)PayTypeEnum.后台处理;
                    }
                   
                    model.RMB = savemodel.RMB;
                    model.Tip = savemodel.Tip;
                    _ordersTask.Update(model);
                }
                return AlertMsg("保存成功", HttpContext.Request.UrlReferrer.PathAndQuery);

            }

            return View(savemodel);
        }



        #region 删除用户信息 Delete

        [HttpPost]
        public void Delete(int id)
        {
            _ordersTask.Delete(id);
        }

        #endregion
    }
}